﻿using System.Collections.Generic;
using Engine;
using Engine.Graphics;
using Game;
using Mekiasm.Interface;

namespace Mekiasm
{
    public class SimpleSeedItem : ItemFlat, GrowableSeed
    {
        public string TextureName;

        public virtual int StateDuration => 1200;

        public virtual int MaxState => 3;

        public virtual int MatureValue { get; set; }

        public virtual int SoilValue { get; set; } = SoilBlock.Index;

        public virtual int WitheredValue { get; set; } = DryBushBlock.Index;

        public virtual bool NeedWater => true;

        public bool NeedNitrogen => true;

        public SimpleSeedItem(string n, string name) : base( n, ContentManager.Get<Texture2D>("Mekiasm/Textures/crops/" + name + "_stage_0"))
        {
            TextureName = name;

            for (int i = 0; i <= MaxState; i++)
            {
                GetTexture(i);
            }
        }
        public override int GetTextureSlotCount(int value)
        {
            return 1;
        }
        
        public override bool IsPlaceable_(int value)
        {
            return true;
        }

        public Texture2D GetTexture(int state)
        {
            return ContentManager.Get<Texture2D>("Mekiasm/Textures/crops/" + TextureName + "_stage_" + state);
        }

        public override void GetDropValues(SubsystemTerrain subsystemTerrain, int oldValue, int newValue, int toolLevel, List<BlockDropValue> dropValues, out bool showDebris)
        {
            int state = ILibrary.getExtraData(oldValue);
            if (state < MaxState)
            {//未成熟
                if (!ILibrary.random.Bool(0.1f)) dropValues.Add(new BlockDropValue() { Count = 1, Value = ILibrary.setExtraData(oldValue, 0) });
            }
            else
            { //已成熟
                int Count = 1;
                if (ILibrary.random.Bool(0.1f)) Count = 2;
                dropValues.Add(new BlockDropValue() { Count = Count, Value = MatureValue });
            }
            showDebris = true;
        }

        public override bool IsFaceTransparent(SubsystemTerrain subsystemTerrain, int face, int value)
        {
            return true;
        }
        public override BoundingBox[] GetCustomCollisionBoxes(SubsystemTerrain terrain, MovingBlock value)
        {
            return new BoundingBox[0];
        }

        public override bool OnUse(Ray3 aim, ComponentMiner componentMiner, TerrainRaycastResult? raycast)
        {
            return false;
        }

        public override void GenerateTerrainVertices(Block block, BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
        {
            int state = ILibrary.getExtraData(value);
            Texture2D texture = GetTexture(state);
            generator.GenerateCrossfaceVertices(block, value, x, y, z, Color.White, 0, geometry.GetGeometry(texture).SubsetAlphaTest);
        }

        public override PlantSystem.PlantItem GetPlantItem(int value, int x, int y, int z)
        {
            return new PlantSystem.PlantItem(x, y, z, blockValue, StateDuration, (item, bottomID, hydration, nitrogen) =>
            {
                bool flag = NeedWater == true && !hydration && random.Bool(0.5f);
                if (!flag && NeedNitrogen && nitrogen == 0 && random.Bool(0.3f)) flag = true;

                if (bottomID == SoilValue && !flag)
                {

                    if (item.GrowState <= MaxState) ILibrary.SubsystemTerrain.ChangeCell(x, y, z, ILibrary.setExtraData(blockValue, item.GrowState));

                    if (item.GrowState == MaxState)
                    {
                        ILibrary.SubsystemTerrain.ChangeCell(x, y, z, MatureValue);
                        item.Remove = true;
                    }
                }
                else
                {//下面不是耕地，枯萎
                    ILibrary.SubsystemTerrain.ChangeCell(x, y, z, WitheredValue);
                    item.Remove = true;
                }
            });
        }

        public override void OnBlockAdded(SubsystemBlockBehavior blockBehavior, int value, int oldValue, int x, int y, int z)
        {
            PlantSystem.Add(GetPlantItem(blockValue, x, y, z));
        }

        public override void OnBlockRemoved(SubsystemBlockBehavior blockBehavior, int value, int newValue, int x, int y, int z)
        {
        }

        public override BlockDigMethod GetDigMethod(int value)
        {
            return BlockDigMethod.Hack;
        }
        public override float GetDigResilience(int value)
        {
            return 0f;
        }

    }
}
